package com.wechat.weibo.service;

import com.wechat.weibo.dao.PickDao;
import com.wechat.weibo.dao.UserDao;
import com.wechat.weibo.entity.Follows;
import com.wechat.weibo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FollowService {
    @Autowired(required = false)
    PickDao pickDao;

    @Autowired(required = false)
    UserDao userDao;

    public int follow(User self,User other){
        Follows selfIsFan=pickDao.showRelation(self.getOpenid(),other.getOpenid());
        Follows otherIsFan=pickDao.showRelation(other.getOpenid(),self.getOpenid());
        if(selfIsFan==null&&otherIsFan==null){
            pickDao.addFan(self,other);
            userDao.addNum(self,other);
        }
        else if(selfIsFan==null||otherIsFan==null){
            return -1;
        }
        else {
            if(selfIsFan.isFan_pick_main())return 1;
            pickDao.updateFan(selfIsFan.getPick_id(),otherIsFan.getPick_id());
            userDao.addNum(self,other);
        }
        return 0;
    }

    public int cancelFollow(User self,User other){
        Follows selfIsFan=pickDao.showRelation(self.getOpenid(),other.getOpenid());
        Follows otherIsFan=pickDao.showRelation(other.getOpenid(),self.getOpenid());
        if(selfIsFan==null&&otherIsFan==null){
            return 1;
        }
        else if(selfIsFan==null||otherIsFan==null){
            return -1;
        }
        else {
            if(!selfIsFan.isFan_pick_main())return 1;
            if(selfIsFan.isPick_back())pickDao.deUpdateFan(selfIsFan.getPick_id(),otherIsFan.getPick_id());
            else pickDao.delFan(selfIsFan.getPick_id(),otherIsFan.getPick_id());
        }
        userDao.delNum(self,other);
        return 0;
    }
}
